/**
 * 将样式配置，结果维度数据映射关系，数据转换为echart的options
 * @param title 标题
 * @param styleOptions
 * @param relativeConfig
 * @param data
 */
function template(title,styleOptions,relativeConfig,data){

    let fieldConfig = {x:'',data:[]};

    for(let index in relativeConfig){

        let relationItem = relativeConfig[index];

        if(relationItem.relationDimension == 'x' ){

            fieldConfig.x = relationItem.resultColumn;
        }else if(relationItem.relationDimension == 'data' ){
            fieldConfig.data.push(relationItem.resultColumn);
        }
    }




    let xKey = fieldConfig.x['columnField'];

    let dataKeys = fieldConfig.data;

    let xValues = [];

    let series=[];

    for(let index in dataKeys){

        series.push({
            field:dataKeys[index]['columnField'],
            name:dataKeys[index]['columnDesc'],
            type:'line',
            data:[]
        });
    }

    for( let index in data){

        let dataitem = data[index];

        xValues.push(dataitem[xKey]);

        for(let sindex in series){

            let serie = series[sindex];

            serie.data.push(dataitem[serie.field]);
        }

    }


    let option = {
        xAxis: {
            type: 'category',
            data:xValues
        },
        yAxis: {
            type: 'value'
        },
        title: {
            text: title
        },
        series: series
    };

    //处理样式


    let styleFunc = function (p,styleItem) {

        let key_l1 = styleItem.key;

        if(! p[key_l1]) {
            p[key_l1] = {};
        }


        if(! Array.isArray(p[key_l1] )){

            //如果不是数组  就直接配置样式

            if(styleItem.type != 'form'){

                p[key_l1] = styleItem.value;
            }else{

                for(let styleItem_l2 of styleItem.items){


                    styleFunc(p[key_l1],styleItem_l2);

                }

            }

        }else{

            //是数组的话 就将item配置样式




            for(let arrayItem of p[key_l1]){


                if(styleItem.type != 'form'){

                    arrayItem = styleItem.value;

                }else{

                    for(let styleItem_l2 of styleItem.items){

                        styleFunc(arrayItem,styleItem_l2);

                    }

                }

            }
        }




    }

    if(Array.isArray(styleOptions) ){

        for(let item of styleOptions){

            styleFunc(option,item);
        }

    }



    return option;

}